using System.Collections.Generic;
using UnityEngine;

public class CarCtrl : RoleInfo
{
    public List<WheelCollider> wheels;
    public List<Transform> wheelModels;
    public float speed = 1000f;
    public Transform center; //重心
    public EnterCar enterCar; //上车检测

    new void Start()
    {
        base.Start();
        GetComponent<Rigidbody>().centerOfMass = center.localPosition; //添加重心
    }

    void Update()
    {
    }

    public override void Move(float h, float v)
    {
        float fMove = v * speed;
        for (int i = 0; i < 2; i++) wheels[i].steerAngle = h * 30;
        for (int i = 0; i < wheels.Count; i++)
        {
            wheels[i].motorTorque = fMove; //油门
        }
        for (int i = 0; i < wheelModels.Count; i++)
        {
            wheels[i].GetWorldPose(out var pos, out var rot); //轮子旋转
            wheelModels[i].position = Vector3.Lerp(wheelModels[i].position, pos, 5f * Time.deltaTime);
            wheelModels[i].rotation = rot;
        }
    }
    public override void die()
    {
        Debug.Log("车辆被摧毁!");
    }

    public override void OnEvent()
    {
        Debug.Log("下车事件!");
        if (enterCar) enterCar.ExitCar();
    }
}
